# -*- coding: utf-8 -*-

__author__ = 'parano1d'
__date__ = '12.07.2010 20:43:04'

import pygtk
import gtk
import gtk.glade
from gestor.packages import GestorPackages

pygtk.require('2.0')

class GUI:
    """GTK interface gor Gestor PM"""

    __glade_main_window = 'glade/ru/main_window.glade'

    def __init__(self):
        """Creates main window of application"""
        
        self.main_window = gtk.glade.XML(self.__glade_main_window)

        window = self.main_window.get_widget('main_window')
        if (window):
            window.connect('destroy', self.close_app)

        self.show_packages()

        gtk.main()
        
    def show_packages(self):
        """Shows packages from database in the treeview"""

        # Load categories, packages and versions from database
        cl_packages = GestorPackages()
        self.categories_full = cl_packages.get_full()
        
        treeview1 = self.main_window.get_widget('treeview1')
        treestore = gtk.TreeStore(str)

        treeview1.connect('cursor-changed', self.treeview_select)

        # Create column for treeview
        tvcolumn = gtk.TreeViewColumn(u'Пакеты')
        treeview1.append_column(tvcolumn)

        cell = gtk.CellRendererText()
        tvcolumn.pack_start(cell, True)
        tvcolumn.add_attribute(cell, 'text', 0)

        # Insert all categories, packages and versions into the tree
        self.tree_path = []
        for category in self.categories_full:
            category_tree = treestore.append(None, [category['title']])

            for package in category['packages']:
                package_tree = treestore.append(category_tree, [package['title']])

                for version in package['versions']:
                    treestore.append(package_tree, [version['title']])

        # Set created model to the treeview
        treeview1.set_model(treestore)

    def edit_item(self, item):
        self.main_window.get_widget('label_title').show()
        entry_title = self.main_window.get_widget('entry_title')
        entry_title.set_text(item['title'])
        entry_title.show()

        if item['type'] == 'category':
            # Description label
            self.main_window.get_widget('label_description').show()

            # Description textview
            textview_description = self.main_window.get_widget('textview_description')
            tb = gtk.TextBuffer()
            tb.set_text(item['description'])
            
            textview_description.set_buffer(tb)
            textview_description.show()
        elif item['type'] == 'package':
            # Description label
            self.main_window.get_widget('label_description').show()

            # Description textview
            textview_description = self.main_window.get_widget('textview_description')
            tb = gtk.TextBuffer()
            tb.set_text(item['description'])

            textview_description.set_buffer(tb)
            textview_description.show()

            # Category label
            self.main_window.get_widget('label_category').show()

            # Category combobox
            combobox_category = self.main_window.get_widget('combobox_category')
            combobox_category.show()
        elif item['type'] == 'version':
            pass

        self.main_window.get_widget('button_save').show()

    def treeview_select(self, treeview):
        path = treeview.get_selection().get_selected_rows()[1][0]

        cur_item = self.categories_full[path[0]]

        try:
            cur_item = cur_item['packages'][path[1]]
        except:
            pass
        else:
            try:
                cur_item = cur_item['versions'][path[2]]
            except:
                pass

        self.edit_item(cur_item)

    def close_app(self, widget):
        """Closes application"""
        
        gtk.main_quit()        
